<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                        <el-button type="warning" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData.data"  border>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column align="center"  prop="name" label="标识" width="180"></el-table-column>
                        <el-table-column prop="title" label="标题" width="200"></el-table-column>
                        <el-table-column prop="keysords" label="关键词" width="300"></el-table-column>
                        <el-table-column prop="describe" label="描述" width="350" :show-overflow-tooltip="true"></el-table-column>
                        <el-table-column prop="created_at" label="创建日期" width="160"></el-table-column>
                        <el-table-column prop="created_at" label="最近修改" width="160"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button @click="destory(scope.row)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item :rules="[{ required: true, message: '请输入SEO唯一标识', trigger: 'blur'}]" label="标识：" prop="name">
                    <el-input  v-model="form.name" placeholder="请输入SEO唯一标识，大写英文字母"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请输入SEO标题', trigger: 'blur'}]" label="名称：" prop="title">
                    <el-input  v-model="form.title" placeholder="请输入SEO标题"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请输入SEO关键词', trigger: 'blur'}]" label="名称：" prop="keysords">
                    <el-input v-model="form.keysords" placeholder="请输入SEO关键词"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请输入SEO描述', trigger: 'blur'}]" label="描述：" prop="describe">
                    <el-input v-model="form.describe" placeholder="请输入SEO描述"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {SeoList,SeoUpdate,SeoDestroy} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page:1,kwd:''},
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '500px'
                },
                form:null,
            }
        },
        activated(){
            this.list();
        },
        computed: {
            ...mapGetters(['active','loading']),
        },
        methods: {
            //新增
            create(){
                this.form = {
                    id:0,
                    title:'',
                    name:'',
                    keysords:'',
                    describe:'',
                };
                this.dialog.title = '新增SEO';
                this.dialog.show = true;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    title: info.title,
                    name: info.name,
                    keysords: info.keysords,
                    describe: info.describe,
                };
                this.dialog.title = '编辑SEO';
                this.dialog.show = true;
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                SeoList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        SeoUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(info){
                this.$confirm('确定要删除该seo信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    SeoDestroy({id: info.id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
        }
    }
</script>

<style scoped>

</style>
